package practice2;

import utils.node.DoubleNode;
import utils.NodeUtils;

/**
 * @author Tara
 * @implNote 给定一个双向链表，如何反转双向链表？
 */
public class Pra3 {

    public static void main(String[] args) {
        Object[] arr = {2, 4, 7, 9, 23, 21, 8 , 43};
        // 构造一个双向链表
        DoubleNode node = NodeUtils.constructDoubleNode(arr);
        // 打印链表轨迹
        NodeUtils.printDoubleNode(node);
        //打印链表结构
        NodeUtils.printDoubleNode2(node);

        System.out.println("=====================反转双向链表======================");
        DoubleNode node1 = reverseDoubleNodeList(node);
        NodeUtils.printDoubleNode(node1);
        NodeUtils.printDoubleNode2(node1);
    }

    //反转双向链表
    public static DoubleNode reverseDoubleNodeList(DoubleNode head){
        DoubleNode preNode = null;
        DoubleNode nextNode = null;
        while (null!=head){
            nextNode = head.nextNode;
            head.nextNode = preNode;
            head.lastNode = nextNode;
            preNode = head;
            head = nextNode;
        }
        return preNode;
    }
}
